#include "ResultRecord.h"

#include <QDebug>

ResultRecord::ResultRecord() :
    ASTM()
{

}

QString ResultRecord::buildRecord() const
{
    QString record = recordTypeSymbol(RecordType::Result);
    record += FIELD;
    record += QString::number(frameIndex);
    record += FIELD;
    record += projectName;
    record += FIELD;
    // 测试结果值（至少含两位小数）
    record += result;
    record += FIELD;
    // 测试项目的单位
    record += unit;
    record += FIELD;

    // 参考值下限
    // 参考值上限 
    if(rangeLower == "0" && rangeUpper == "0") {
        record += FIELD;
        record += FIELD;
    }
    else {
        record += rangeLower;
        record += FIELD;
        record += rangeUpper;
        record += FIELD;
    }

    // 测试时间
    record += testDateTime;
    record += FIELD;
    // 结果标志
    record += flagList.value(flag);
    record += FIELD;
    // 结果提示
    record += resultTip;
    record += CR;

    return record;
}

bool ResultRecord::analyzeRecord(const QString &str)
{
    QStringList fields = str.split(FIELD);

    if(fields.count() < 9 || fields.value(0) != recordTypeSymbol(RecordType::Result)) {
        // qWarning() << "ResultRecord: Syntax Error!";
        return false;
    }

    projectName = fields[2];
    result = fields[3];
    unit = fields[4];
    rangeLower = fields[5];
    rangeUpper = fields[6];
    testDateTime = fields[7];

    if(flagList.indexOf(fields[8]) >= 0) {
        flag = static_cast<ResultFlag>(flagList.indexOf(fields[8]));
    }

    resultTip = fields[9];

    return true;
}
